﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Price
    {
        public static void insert(PriceEntity price)
        {
            DbHelper.ExecuteNonQuery("p_Price_insert",
              price.CompanyId,
              price.AdminId,
              price.ProductId,
              price.Price,
              price.SellerPrice,
              price.Unit
               );
        }

        public static void updateByProduct(PriceEntity price)
        {
            DbHelper.ExecuteNonQuery("p_Price_updateByProduct",
              price.CompanyId,
              price.ProductId,
              price.Price,
              price.SellerPrice,
              price.Unit
               );
        }

        public static void updateByAdmin(PriceEntity price)
        {
            DbHelper.ExecuteNonQuery("p_Price_updateByAdmin",       
              price.ProductId,
              price.Price,
              price.BusinessNature,
              price.AdminId
               );
        }

        public static PriceEntity getByProduct(int productId,int companyId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_Price_getByProduct", productId, companyId);
            PriceEntity price = new PriceEntity();
            if (dr != null)
            {
                price.PriceId = Convert.ToInt32(dr["priceId"]);
                price.CompanyId = Convert.ToInt32(dr["companyId"]);
                price.AdminId = Convert.ToInt32(dr["adminId"]);
                price.ProductId = productId;
                price.Price = Convert.ToDecimal(dr["price"]); 
                price.SellerPrice = Convert.ToDecimal(dr["sellerPrice"]); 
                price.Unit = dr["unit"].ToString();
                price.InputDt = Convert.ToDateTime(dr["inputDt"]); 
            }
            return price;
        }

        public static PriceEntity getByProduct(int productId, string businessnature)
        {
            DataRow dr = DbHelper.ExecuteDataRow("[p_Price_getByProductIdBusinessNature]", productId, businessnature);
            PriceEntity price = new PriceEntity();
            if (dr != null)
            {
                price.PriceId = Convert.ToInt32(dr["priceId"]);
                price.CompanyId = Convert.ToInt32(dr["companyId"]);
                price.AdminId = Convert.ToInt32(dr["adminId"]);
                price.ProductId = productId;
                price.Price = Convert.ToDecimal(dr["price"]);
                price.SellerPrice = Convert.ToDecimal(dr["sellerPrice"]);
                price.Unit = dr["unit"].ToString();
                price.InputDt = Convert.ToDateTime(dr["inputDt"]);
            }
            return price;
        }
  
        public static decimal getPriceByProductAndCompany(int productId,int companyId)
        {
           string price=DbHelper.ExecuteScalar("p_Price_getPriceByProductAndCompany", productId,companyId).ToString();
           if (price == "")
           {
               return Convert.ToDecimal(0);
           }
           else
           {
               return Convert.ToDecimal(price);
           }
        }

        
        public static DataSet getSupplyPriceList()
        {
            return DbHelper.ExecuteReDataSet("p_Price_supplyPriceList");
        }

        //修改供应商[p_Price_changeSellerInfor]
        public static void changeSellerInfor(int psId,int adminid)
        {
            DbHelper.ExecuteNonQuery("p_Price_changeSellerInfor", psId,adminid);
        }

        public static DataTable listByWhere(string where)
        {
            return DbHelper.ExecuteDataTable("p_price_listByWhere", where);
        }

        public static DataTable listWithBusinessNatures(int productId, int companyId)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Price_ListWithBusinessNatures]", productId, companyId);
        }

        public static DataTable listWithAllParentCategory()
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Price_Interface_ListWithAllParentCategory]");
        }
    }
}
